package com.itheima.homework.homework03;

import java.util.Arrays;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        String[] strings = {"111", "222", "333", "444", "555", "666"};
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            arr[i] = Integer.parseInt(strings[i]);
        }
        System.out.println(Arrays.toString(arr));

        System.out.println("请输入要查找的元素:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        // 手写二分查找方式
//        int index = binarySearch(arr, num);
        // 通过Arrays.binarrySearch()实现
        int index = Arrays.binarySearch(arr, num);

        if (index > -1) {
            System.out.println(num + "是系统中存储的第" + (index + 1) + "个用户！");
        } else {
            System.out.println(num + "不是系统中的用户！");
        }

    }

//    //手写二分查找
//    private static int binarySearch(int[] arr, int num) {
//        int min = 0;
//        int max = arr.length - 1;
//
//        while (min <= max) {
//            int mid = (min + max) / 2;
//
//            if (arr[mid] > num) {
//                max = mid - 1;
//            } else if ((arr[mid] < num)) {
//                min = mid + 1;
//            } else {
//                return mid;
//            }
//        }
//        return -1;
//    }
}
